package game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

class Level_01 extends Scene
{
	Background background;
	Player p;
	Enemy e;
	
	Background saldoInsuficiente;
	Background voceInvestiu;
	
	boolean sInsuficiente = false;
	boolean vcInvestiu = false;
	
	Font font;
	
	Bar barSaude;
	Bar barSaneamento;
	Bar barEducacao;
	Bar barTurno;
	Bar barDominacao;
	
	int dominacao = 206;
	int invetimentoSaude = 0;
	int invetimentoSaneamento = 0;
	int invetimentoEducacao = 0;
	float timeArmy;
	int turnos = 0;
	public static int sizeBarTurno = 0;
	Scene investimentoBolsa;
	
	public float time = 0;
	
	public float timeRemaining = 0;
	
	float temp = 120;
	
	boolean newTurno;
	
	int numeroTurnos = 0;
	
	public Level_01()
	{
		background = new Background(2, new Point(0, 0), new Point(800, 600));
		background.setImage(2);
		p = new Player(100,100,100,100);
		
		voceInvestiu = new Background(16, new Point(0,0), new Point(800, 600));
		voceInvestiu.setImage(16);
		saldoInsuficiente = new Background(15, new Point(0,0), new Point(800, 600));
		saldoInsuficiente.setImage(15);
		
		barSaude = new Bar(7, new Point(563, 474), new Point(215, 8), 215);
		barSaneamento = new Bar(7, new Point(563, 521), new Point(215, 8), 215);
		barEducacao = new Bar(7, new Point(563, 568), new Point(215, 8), 215);
		barTurno = new Bar(7, new Point(43, 9), new Point(184, 8), 184);
		barDominacao = new Bar(7, new Point(360, 9), new Point(413, 9), 413);
		barDominacao = new Bar(17, new Point(360, 9), new Point(413, 9), 413);
		
		e = new Enemy();
		font = new Font("Arial", Font.BOLD, 12);
		
		newTurno = false;
	}
	public void update()
    {
    	MouseVerification();
    	
    	barSaude.UpdateBar(215, new Point(563, 492), invetimentoSaude);
    	barSaneamento.UpdateBar(215, new Point(563, 538), invetimentoSaneamento);
    	barEducacao.UpdateBar(215, new Point(563, 585), invetimentoEducacao);
    	barTurno.UpdateBar(184, new Point(43, 25), sizeBarTurno);
    	barDominacao.UpdateBar(413, new Point(360, 25), dominacao);
    	
    	if(p.possoReduzir == true)
    	{
    		dominacao -=30 * p.intReduzir;
    		p.possoReduzir = false;
    	}
    	
    	if(dominacao <= 0)
    	{
    		SceneManager.changeScene(6);
    	}
    	
    	if(e.possoReduzir == true)
    	{
    		dominacao += 30;
    		e.possoReduzir = false;
    	}
    	
    	if(dominacao >= 413)
    	{
    		SceneManager.changeScene(7);
    	}
    	
    	time += Game.loopTime;
    	
    	timeArmy += Game.loopTime;

    	temp ++;
    	
    	if(numeroTurnos != turnos)
    	{
    		for( Tile t : p.Army)
    		{
    			t.setAddedPosition(60);
    		}
    		for(Tile t : e.Army)
    		{
    			t.setAddedPosition(-60);
    		}
    		Player.patrimonio +=((30000 + p.getEducation() * 100) + (30000 + p.getSanitation() * 160) + (30000 + p.getHealth() * 200));
    		turnos = numeroTurnos;
    	}
    	if(time >= 0.3f)
    	{
    		sizeBarTurno ++;
    		time = 0;
    		newTurno = false;
    	}    	    	
    	if(sizeBarTurno >= 184)
    	{
    		sizeBarTurno = 0;
    		newTurno = true;
    		numeroTurnos++;
    	}
   	
    	if (invetimentoSaude >= 215)
    		invetimentoSaude = 215;
    	
    	if (invetimentoSaneamento >= 215)
    		invetimentoSaneamento = 215;
    	
    	if (invetimentoEducacao >= 215)
    		invetimentoEducacao = 215;
    	
    	if(Player.patrimonio <= 0)
    	{
    		Player.patrimonio = 0;
    		invetimentoSaude = 0;
    		invetimentoSaneamento = 0;
    		invetimentoEducacao = 0;
    		SceneManager.changeScene(6);
    	}
    	p.Update(this);
    	e.Update(this);
    	if(investimentoBolsa != null)
    		investimentoBolsa.update();
    }
	
	public void draw(Graphics2D g2d)
    {
		background.draw(g2d);
		
		g2d.drawString("" + Player.patrimonio,90,495);
		g2d.setColor(Color.BLACK);
		
		g2d.setFont(font);
		
		g2d.drawString("30000", 220,580);
		g2d.drawString("55000", 350,580);
		g2d.drawString("90000", 480,580);
		
		barSaude.draw(g2d);
		barSaneamento.draw(g2d);
		barEducacao.draw(g2d);
		barDominacao.draw(g2d);
		barTurno.draw(g2d);
		p.Draw(g2d);
		e.Draw(g2d);
		
		if(vcInvestiu)
		{
			timeRemaining ++;
			voceInvestiu.draw(g2d);
		}
		if(sInsuficiente)
		{
			timeRemaining ++;
			saldoInsuficiente.draw(g2d);
		}
		
		if(timeRemaining >= 120)
		{
			timeRemaining = 0;
			vcInvestiu = false;
			sInsuficiente = false;
			
		}
		if(investimentoBolsa != null)
			investimentoBolsa.draw(g2d);
    }
	
	public void MouseVerification()
	{
		if (new Rectangle(65,539,59,48).contains(Mouse.getInstance().getPositionMouse().getX(),
				Mouse.getInstance().getPositionMouse().getY()))
		{
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
			{
				if(investimentoBolsa == null)
				investimentoBolsa = new InvestimentoBolsa();
				
			}
		}
		//Botao de voltar.
				if (new Rectangle(610, 546, 68, 32).contains(Mouse.getInstance().getPositionMouse().getX(),
						Mouse.getInstance().getPositionMouse().getY()) && investimentoBolsa != null)
				{
					if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
					{
						investimentoBolsa = null;
					}
				}
		else if(new Rectangle(217,477,64,76).contains(Mouse.getInstance().getPositionMouse().getX(),
				Mouse.getInstance().getPositionMouse().getY())&& p.isRunning == false)
		{
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1) && timeArmy >1)
			{
				if(Player.patrimonio - 30000 > 0)
				{
				int sizeList = p.Army.size();
				timeArmy = 0;
				p.AddArmy(new Troop());
				if(p.Army.size() > sizeList)
				{
					Player.patrimonio -= 30000;
				}
			}
			else
				{
					sInsuficiente = true;
				}
			}
		}
		else if(new Rectangle(301,476,119,74).contains(Mouse.getInstance().getPositionMouse().getX(),
				Mouse.getInstance().getPositionMouse().getY()) && p.isRunning == false)
		{
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1) && timeArmy > 1)
			{
				if(Player.patrimonio - 50000 > 0)
				{
					int sizeList = p.Army.size();
					timeArmy = 0;
					p.AddArmy(new Ship());
					if(p.Army.size() > sizeList)
					{
						Player.patrimonio -= 50000;
					}
				}
				else
					sInsuficiente = true;
			}
		}
		else if(new Rectangle(439,479,105,71).contains(Mouse.getInstance().getPositionMouse().getX(),
				Mouse.getInstance().getPositionMouse().getY())&& p.isRunning == false)
		{
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1) && timeArmy > 1)
			{
				if(Player.patrimonio - 90000 >0)
				{
					int sizeList = p.Army.size();
					timeArmy = 0;
					p.AddArmy(new Submarine());
					if(p.Army.size() > sizeList)
					{
						Player.patrimonio -= 90000;
					}
				}
				else
					sInsuficiente = true;
			}
		}		
		else if(new Rectangle(728,442,60,29).contains(Mouse.getInstance().getPositionMouse().getX(),
				Mouse.getInstance().getPositionMouse().getY()))
		{
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1) && temp > 120)
			{
				temp = 0;
				if(Player.patrimonio >= 50000)
				{
					Player.patrimonio -= 50000;
					invetimentoSaude = invetimentoSaude + 10;
					vcInvestiu = true;
				}
				else if(Player.patrimonio < 50000 && Player.patrimonio> 0)
				{
					sInsuficiente = true;
				}
			}
		}
		else if(new Rectangle(728,488,60,29).contains(Mouse.getInstance().getPositionMouse().getX(),
				Mouse.getInstance().getPositionMouse().getY()))
		{
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1) && temp > 120)
				{
					temp = 0;
					if(Player.patrimonio >= 50000)
					{
						vcInvestiu = true;
						Player.patrimonio -= 50000;
						System.out.println("Investiu no saneamento Basico");
						invetimentoSaneamento = invetimentoSaneamento + 10;
					}
					else if(Player.patrimonio < 50000 && Player.patrimonio> 0)
					{
						sInsuficiente = true;
					}
				}
		}
		else if(new Rectangle(728,534,60,29).contains(Mouse.getInstance().getPositionMouse().getX(),
				Mouse.getInstance().getPositionMouse().getY()))
		{
			if (Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1) && temp > 120)
				{
					temp = 0;
					if(Player.patrimonio > 50000)
					{
						vcInvestiu = true;
						Player.patrimonio -= 50000;
						invetimentoEducacao = invetimentoEducacao + 10;
					}
					else if(Player.patrimonio < 50000 && Player.patrimonio> 0)
					{
						sInsuficiente = true;
					}
				}
		}
	}
}
